package com.tech.distribute_id.snowflake.baidu;

import com.baidu.fsg.uid.UidGenerator;
import com.baidu.fsg.uid.impl.CachedUidGenerator;
import com.baidu.fsg.uid.impl.DefaultUidGenerator;
import com.baidu.fsg.uid.worker.DisposableWorkerIdAssigner;
import com.baidu.fsg.uid.worker.WorkerIdAssigner;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 下载源码，install https://gitee.com/green01/uid-generator.git

 DROP TABLE IF EXISTS WORKER_NODE;
 CREATE TABLE WORKER_NODE
 (
 ID BIGINT NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',
 HOST_NAME VARCHAR(64) NOT NULL COMMENT 'host name',
 PORT VARCHAR(64) NOT NULL COMMENT 'port',
 TYPE INT NOT NULL COMMENT 'node type: ACTUAL or CONTAINER',
 LAUNCH_DATE DATE NOT NULL COMMENT 'launch date',
 MODIFIED datetime NOT NULL COMMENT 'modified time',
 CREATED datetime NOT NULL COMMENT 'created time',
 PRIMARY KEY(ID)
 )
 COMMENT='DB WorkerID Assigner for UID Generator',ENGINE = INNODB;
 */
@Configuration
@MapperScan("com.baidu.fsg.uid.worker.dao")// 将指定范围interface均映射为mapper
public class UidGeneratorConfig {
    @Autowired
    private UidGeneratorProperties properties;

    @Bean
    public WorkerIdAssigner disposableWorkerIdAssigner(){
        DisposableWorkerIdAssigner id = new DisposableWorkerIdAssigner();
        return id;
    }

    @Bean
    @ConditionalOnProperty(name="uid.mode", havingValue="default", matchIfMissing=true)
    public UidGenerator defaultUidGenerator(){
        DefaultUidGenerator defaultUidGenerator = new DefaultUidGenerator();
        defaultUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner());
        defaultUidGenerator.setTimeBits(properties.getTimeBits());
        defaultUidGenerator.setSeqBits(properties.getSeqBits());
        defaultUidGenerator.setWorkerBits(properties.getWorkerBits());
        defaultUidGenerator.setEpochStr(properties.getEpochStr());
        return defaultUidGenerator;
    }

    @Bean
    @ConditionalOnProperty(name="uid.mode", havingValue="cache")
    public UidGenerator cachedUidGenerator(){
        CachedUidGenerator cachedUidGenerator = new CachedUidGenerator();
        cachedUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner());
        cachedUidGenerator.setTimeBits(properties.getTimeBits());
        cachedUidGenerator.setSeqBits(properties.getSeqBits());
        cachedUidGenerator.setWorkerBits(properties.getWorkerBits());
        cachedUidGenerator.setEpochStr(properties.getEpochStr());

        RingBufferProperties ringBuffer = properties.getRingBuffer();
        cachedUidGenerator.setBoostPower(ringBuffer.getBoostPower());
        cachedUidGenerator.setScheduleInterval(ringBuffer.getScheduleInterval());
//        cachedUidGenerator.setRejectedPutBufferHandler(ringBuffer.getBoostPower());
//        cachedUidGenerator.setRejectedTakeBufferHandler(ringBuffer.getBoostPower());
        return cachedUidGenerator;
    }
}
